/**
 * Promise异步请求
 * Promise all
 * fetch
 */


const posts=[
    {title:"Post one",body:"this is post one"},
    {title:"Post two",body:"this is post two"}
];

function getPosts(){
    setTimeout(()=>{
        let output='';
        posts.forEach(post=>{
            output+=`<li>${post.title}</li>`
        });
        document.body.innerHTML=output;
    },1000)
}

function createPost(post){
    //实例化promise对象
    return new Promise((resovle,reject)=>{
        setTimeout(()=>{
            posts.push(post);

            const error=false;

            if(!error){
                resovle();
            }else{
                reject("Error:something went wrong!")
            }
        },2000)
    })
}


// createPost({title:"Post Three",body:"this is post three"})
//     .then(getPosts)
//     .catch(err=>console.log(err))

//Promise.all

const promise1=Promise.resolve("Hello World");
const promise2=Promise.resolve("Hello world");
const promise3=new Promise((resovle,reject)=>{
    setTimeout(resovle,2000,"goodbye")  
})
const promise4=fetch
("https://jsonplaceholder.typicode.com/users").then
(res=>res.json());
Promise.all([promise1,promise2,promise3,promise4]).then
(values=>{
    console.log(values)
})

